//
//  YiHttpRequest.swift
//  yiplayer
//
//  Created by cgw on 2024/5/9.
//

import Foundation

public let YIHttp = YiHttpRequest.shared
open class YiHttpRequest : NSObject{
    static let shared: YiHttpRequest = {
        return YiHttpRequest()
    }()
    
    // MARK: - -
    func comError(msg: String, complete:@escaping (Any?, NSError?) -> Void) {
        let err = BLError.error(msg: msg)
        complete(nil, err)
    }
    
    // MARK: - 下载文件
    func download(url: String, toDir:String, fileName: String, progressBlock:@escaping (((Float)->Void)), complete:@escaping (String?, NSError?) -> Void) {
        BLHttpRequest.sharedInstance.download(url: url, toDir: toDir, fileName: fileName, requestModifier: { req in
            
        }) { progress in
            let count = progress.totalUnitCount
            
            let jd = Float(progress.completedUnitCount)/Float(count)
            progressBlock(jd)
            
        } complete: { filePath, err in
            DispatchQueue.main.async {
                complete(filePath,err)
            }
        }
    }
    
    // MARK: - 数据解析
    func analysisData(data: Any?) -> (Any?, NSError?){
        var err : NSError?
        let data = data as? Data
        if let d = data {
            do{
                let dic = try JSONSerialization.jsonObject(with: d, options: JSONSerialization.ReadingOptions.mutableContainers)
                
                if dic is [String :Any] {
                    return (dic, nil)
                }
                
                
                if dic is Dictionary<String, Any> {
                    let tdic = dic as! Dictionary<String, Any>
                    if tdic.isEmpty==false {
                        let code = tdic["code"]
                        
                        if let code = code {
                            
                            if let c = code as? Int {
                                if c == 200 {
                                    return (tdic["data"], nil)
                                }else{
                                    var mg = "操作失败"
                                    if let msg = tdic["msg"] as? String {
                                        mg = msg
                                    }
                                    err = BLError.error(msg: mg)
                                    return (nil, err)
                                }
                            }
                            
                            if code is String {
                                
                                //返回码和错误消息都为空，则也认为是成功
                                var isCodeNilAndErrNil = false
                                if code as! String == "" {
                                    let errMsg = tdic["msg"]
                                    if let msg = errMsg {
                                        if msg is String {
                                            if msg as! String == "" {
                                                isCodeNilAndErrNil = true
                                            }
                                        }
                                    }
                                }
                                
                                if code as! String == "success" {
                                    return (tdic["data"], err)
                                }else if isCodeNilAndErrNil {
                                    
                                    var d = tdic["data"]
                                    if let d1 = d as? String {
                                        if d1 == "" {
                                            d = tdic["rtnResult"]
                                        }
                                    }
                                    return (d, err)
//                                    return (tdic["data"], err)
                                }
                                else {
                                    let errMsg = tdic["msg"]
                                    if let msg = errMsg {
                                        if msg is String {
                                           
                                            err = BLError.error(msg: msg as! String)
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch{
            
                print("json解析失败")
                err = BLError.error(msg: "数据解析失败")
            }
        }
        
        if err == nil {
            err = BLError.error(msg: "数据解析失败")
        }
        return (nil, err)
    }
}


